namespace eval ::hmtable {
  variable _vars;
  array set _vars {w .top row 0}
};

proc ::hmtable::ValidateValue {args} {
    puts [info level 0]
    return 1
}

proc ::hmtable::SetValue {args} {
    puts [info level 0]
    return 1
}

proc ::hmtable::GetMaterials {args} {
    return "mat1 mat2 mat3 mat4 mat5 mat6 mat7 mat8 mat9 mat10 mat11 mat12"
}

proc ::hmtable::GetProperties {args} {
    return "prop1 prop2 prop3 prop4 prop5 prop6 prop7 prop8 prop9 prop10 prop11 prop12"
}

proc ::hmtable::SetToDefaultValues {w C} {
    foreach id [$w rowlist] {
        $w cellset $id,$C 0
    }
}

### valid options are histogram filesave choosedir uint str combobox intcheck boolcheck rgbcolor int editablecombobox real multiselectcombobox fileopen intcolor image hexcolor
proc ::hmtable::CreateColumns {w} {
	$w columncreate test -text "TestData" -type fileopen -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
    $w columncreate result -text "Result" -type fileopen -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate subcase -text "Subcase" -type uint -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate simulation -text "Simulation" -type uint -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate mode -text "Display Mode" -type combobox -valuelistcommand "::hmtable::GetMaterials" -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate data -text "Data" -type combobox -valuelistcommand "::hmtable::GetMaterials" -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate topn -text "TopN" -type uint -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate above -text "Above" -type real -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate base -text "DevBase" -type combobox -valuelistcommand "::hmtable::GetMaterials" -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate dev -text "Dev < " -type real -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
	$w columncreate h3d -text "H3D" -sort 0 -validatecommand ::hmtable::ValidateValue -valueaccept "::hmtable::SetValue %W %I %C %V %P"
}

proc ::hmtable::AddRow { args } {
	variable _vars
	incr _vars(row)
	$_vars(w).midframe.table rowinsert end row$_vars(row) -values [list test test$_vars(row) result result$_vars(row) subcase 1 simulation 0 mode All data U-TestOrFEA topn 10 above 0.25 base max dev 0.3 h3d h3d$_vars(row)]
}

proc ::hmtable::DeleteRow { args } {
	variable _vars
	$_vars(w).midframe.table rowdelete [lindex [split [lindex [$_vars(w).midframe.table selectionget] 0] ","] 0]
}

proc ::hmtable::OpenTable { args } {
	puts opentable
}

proc ::hmtable::SaveTable { args } {
	puts savetable
}

proc ::hmtable::WriteOutH3d { args } {
	puts WriteOutH3d
}

proc ::hmtable::GUI { args } {
	variable _vars
	array set ::hmtable::_vars {dataType mises dataTypeList {mises displacement}}
	
	set w $_vars(w)
	catch {destroy $w}
	toplevel $w -borderwidth 3
	wm title $w "hwtk::treetable for hm table entity"
	set width 800
	set height 400
	set x [expr {([winfo screenwidth .]-$width)/2}]
	set y [expr {([winfo screenheight .]-$height)/2}]

	wm geometry $w ${width}x${height}+$x+$y
	wm transient $w .
	
	set bWidth  10
	pack [frame $w.topframe] -fill x -pady 4 
	pack [label $w.topframe.label -text "H3d output dir:" ] -side left
	pack [hwtk::choosedirentry $w.topframe.dir -textvariable ::hmtable::_vars(dir) ] -side left -expand 1 -fill x
	pack [button $w.topframe.button -text "write out" -width $bWidth -command "::hmtable::WriteOutH3d"] -side right -padx 4

	pack [frame $w.contourframe] -fill x -pady 4 
	pack [hwt::AddEntry  $w.contourframe.type \
				-label "Result type:" \
				-labelwidth 10 \
				-textvariable ::hmtable::_vars(dataType) \
				-state normal \
				-listVar notyping fromPopDown ::hmtable::_vars(dataTypeList) \
				-entryWidth 12 \
				] -side left
				
	pack [hwt::AddEntry  $w.contourframe.comp \
				-textvariable ::hmtable::_vars(dataCompoment) \
				-state normal \
				-listVar notyping fromPopDown ::hmtable::_vars(dataCompomentList) \
				-entryWidth 12 \
				] -side left
				
	pack [hwt::AddEntry  $w.contourframe.layer \
				-label "Layers:" \
				-labelwidth 6 \
				-textvariable ::hmtable::_vars(dataLayer) \
				-state normal \
				-listVar notyping fromPopDown ::hmtable::_vars(dataLayerList) \
				-entryWidth 6 \
				] -side left
				
	pack [hwt::AddEntry  $w.contourframe.average \
				-label "Averaging method:" \
				-labelwidth 15 \
				-textvariable ::hmtable::_vars(averageMethod) \
				-state normal \
				-listVar notyping fromPopDown ::hmtable::_vars(averageMethodList) \
				-entryWidth 15 \
				] -side left
	pack [checkbutton $w.contourframe.corner -text "Use corner data" -variable ::hmtable::_vars(useCorner) ] -side left

	pack [frame $w.midframe] -fill both -expand true
	pack [frame $w.midframe.tool] -fill x -pady 4
	pack [button $w.midframe.tool.b1 -text "Add" -width $bWidth -command "::hmtable::AddRow" ] -side left
	pack [button $w.midframe.tool.b2 -text "Delete" -width $bWidth -command "::hmtable::DeleteRow" ] -side left -padx 4
	pack [::hwtk::table $w.midframe.table -columndnd false -closeeditor true ] -fill both -pady 4 -expand true
	
	pack [frame $w.botframe] -fill x -pady 4 -side bottom
	pack [button $w.botframe.b1 -text "Open..." -width $bWidth -command "::hmtable::OpenTable"] -side left
	pack [button $w.botframe.b2 -text "Save..." -width $bWidth -command "::hmtable::SaveTable"] -side left -padx 4

	::hmtable::CreateColumns $w.midframe.table
	puts $w.midframe.table

}

::hmtable::GUI
#~ ::post::GetPostHandle handp
#~ hwi GetActiveClientHandle handc

#~ hwi OpenStack;

	#~ #do nothing if no model loaded or error
	#~ set err [::post::GetActiveModelHandle handm$ui];
	#~ if { $err != "success" } {
		#~ hwi CloseStack;
		#~ set m_preview_eid 0;
		#~ set m_preview_sys {};

		#~ return [tk_messageBox \
			#~ -title "Attention"\
			#~ -icon info \
			#~ -message "Please load model." \
			#~ -type ok]
	#~ }
#~ ::post::GetActiveModelHandle handm
#~ handm GetResultCtrlHandle handrc
